﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using DDay.iCal;
using DDay.iCal.Components;

namespace Calendar
{
    public class HACalendar
    {
        static char[] splt = { ':' };

        public static iCalendar GetCal()
        {
            Uri uri = new Uri("https://calendar.homeawayconnect.com/icalendar/hacalendar.htm?cid=4b6424cf2e45428ab80a693a84922fc9");


            WebRequest req = WebRequest.Create(uri);
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            iCalendar iCal = new iCalendar();
            Event evt = null;
            using (StreamReader sr = new StreamReader(stream))
            {
                while (sr.Peek() >= 0)
                {
                    string s = sr.ReadLine();
                    string[] it = s.Split(splt);
                    if (it[0].Equals("DTSTART"))
                    {
                        evt.Start = new DDay.iCal.DataTypes.iCalDateTime(it[1]);
                    }
                    if (it[0].Equals("DTEND"))
                    {
                        evt.End = new DDay.iCal.DataTypes.iCalDateTime(it[1]);
                    }
                    if (s.Trim().Equals("SUMMARY:RESERVE"))
                    {
                        evt.Name = "Booking";
                        evt.Summary = "RESERVE";
                    }
                    if (s.Trim().Equals("END:VEVENT"))
                    {
                        if (evt.Name.Equals("Booking"))
                            iCal.AddChild(evt);
                        else
                        {
                            iCal.RemoveChild(evt);
                        }

                    }
                    if (s.Trim().Equals("BEGIN:VEVENT"))
                    {
                        evt = iCal.Create<Event>();
                    }
                }
            }
            return iCal;
        }
        
    }
}
 